package utils

import "sync"

type SafeStrings struct {
	lock *sync.RWMutex
	list []string
}

func NewSafeStrings() *SafeStrings {
	rval := &SafeStrings{
		lock: new(sync.RWMutex),
		list: make([]string, 0, 4096),
	}
	return rval
}

func (this *SafeStrings) Push(data string) {
	this.lock.Lock()
	defer this.lock.Unlock()
	this.list = append(this.list, data)
}

func (this *SafeStrings) GetCount() int {
	return len(this.list)
}

func (this *SafeStrings) ExtractList(max_num int, spliter string, prefix, endfix string) string {
	this.lock.Lock()
	defer this.lock.Unlock()
	l := len(this.list)
	if l == 0 {
		return ""
	}

	str := prefix

	i := 0
	for {
		if i >= l {
			str += endfix
			this.list = make([]string, 0, 4096)
			break
		} else if i >= max_num {
			str += endfix
			tmp := make([]string, 0, 4096)
			this.list = append(tmp, this.list[i:]...)
			break
		} else {
			itmStr := this.list[i]
			if i > 0 {
				str += spliter
			}
			str += itmStr
			i++
		}
	}

	return str

}

func (this *SafeStrings) Start() {

}
